<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 同步网站数据
 * @author zxj
 * @description
 */
class SitesSync extends CI_Controller {
	function __construct()
    {
        parent::__construct();
        //加载模型
        $this->load->model('order_model');
        $this->load->model('product_model');
        $this->load->model('brand_model');
        //加载同步接口类
        $params = gp();
        $params['sitePrefix'] = $params['sitePrefix'] ? $params['sitePrefix'] : 'es';
        $this->load->library('tomrepair', $params);
        //分析器
        //$this->output->enable_profiler(TRUE);
    }



    /**
     * 同步网站上的订单
     * @param int $is_sync
     * @param int $pageSize
     */
    function syncOrder($is_sync = 0,$pageSize = 100)
    {
        $id = gp('id');
        if($id){
            $this->_conductOrder($id,$is_sync);
        }else{
            $orderList = $this->tomrepair->getOrder($is_sync, $pageSize);
            foreach ($orderList as $val) {
                $this->_conductOrder($val,$is_sync);
            }
        }
    }


    /**
     * 编辑更新订单
     * @param int $is_sync
     * @param int $pageSize
     */
    function editOrder($is_sync = 1,$pageSize = 100)
    {
        $id = gp('id');
        if($id){
            $this->_updateOrder($id,$is_sync);
        }else{
            $orderList = $this->tomrepair->getOrder($is_sync, $pageSize);
            foreach ($orderList as $val) {
                $this->_updateOrder($val,$is_sync);
            }
        }
    }



    /**
     * 处理获取到的数据信息
     * @param string $id
     * @param int $is_sync
     * @return bool
     */
    function _conductOrder($id = '',$is_sync = 0){
        if(!$id){
            echo 'id not null';
            return false;
        }
        $allOrder = $this->tomrepair->getOrderDetail($id, $is_sync);
        $orderInfo = $allOrder['order']; //订单数据
        $productList = $allOrder['products']; //产品数据
        //判断是否存在，如果存在则修改
        $where = array(
            'repair_site_id' => $orderInfo['repair_site_id'],
            'source_int_id' => $orderInfo['source_int_id']
        );
        gsh_output('-----------------组织好的数据-------------------');
        gsh_output($allOrder);
        $orderFirst = $this->order_model->selectFirst('maintain_order', $where);
        if ($orderFirst && $orderFirst['maintain_id']) {
        //if(1==2){
            //更新订单
            $this->order_model->addOrSetOrder($orderInfo, $orderFirst['maintain_id']);
            //更新产品
            foreach ($productList as $row) {
                $row['maintain_id'] = $orderFirst['maintain_id'];
                $where = array('maintain_id' => $orderFirst['maintain_id'], 'source_product_id' => $row['source_product_id']);
                $productFirst = $this->common_model->selectFirst('maintain_product', $where);
                $this->product_model->addOrSetProduct($row, $productFirst['product_id']);
            }
        } else {
            //生成维修单号
            $oid = $this->order_model->createRepairId($orderInfo['repair_site_id']);
            $orderInfo['maintain_id'] = $oid;
            $this->order_model->addOrSetOrder($orderInfo);
            //添加产品
            foreach ($productList as $row) {
                $row['maintain_id'] = $oid;
                for($i=1;$i<=$row['number'];$i++){
                    $this->product_model->addOrSetProduct($row);
                }
            }
        }
        //修改网站的订单状态
        $this->siteOrderSync($orderInfo['source_id']);
    }


    /**
     * 处理获取到的数据信息
     * @param string $id
     * @param int $is_sync
     * @return bool
     */
    function _updateOrder($id = '',$is_sync = 1)
    {
        if (!$id) {
            echo 'id not null';
            return false;
        }
        $allOrder = $this->tomrepair->getOrderDetail($id, $is_sync);
        $orderInfo = $allOrder['order']; //订单数据
        $productList = $allOrder['products']; //产品数据
        //判断是否存在，如果存在则修改
        $where = array(
            'repair_site_id' => $orderInfo['repair_site_id'],
            'source_int_id' => $orderInfo['source_int_id']
        );
        gsh_output('-----------------组织好的数据-------------------');
        gsh_output($allOrder);
        $orderFirst = $this->common_model->selectFirst('maintain_order', $where);
        if ($orderFirst) {
            //更新订单
            if ($orderFirst['consumer_name'] == '{{{') {
                $this->common_model->update('maintain_order', $orderInfo, array('maintain_id' => $orderFirst['maintain_id']));
            }
            //更新产品
            foreach ($productList as $row) {
                $row['maintain_id'] = $orderFirst['maintain_id'];
                $where = array('maintain_id' => $orderFirst['maintain_id'], 'source_product_id' => $row['source_product_id']);
                $productFirst = $this->common_model->selectFirst('maintain_product', $where);
                if ($productFirst) {
                    $setData = array('weixiudan_order_id' => $row['weixiudan_order_id']);
                    $this->common_model->update('maintain_product', $setData, $where);
                }
            }
        }
    }



    /**
     * 同步状态
     * @param string $source_id 原网站ID
     */
    function siteOrderSync($source_id = ''){
        if($source_id){
           $this->tomrepair->updateData($source_id, array('is_sync'=>'1'), 'someorder');
        }else{
            $orderAll = $this->order_model->getOrderData();
            foreach ($orderAll as $rs) {
                $source_id = intval($rs['source_id']);
                if(!$source_id) continue;
                $this->tomrepair->updateData($source_id, array('is_sync'=>'1'), 'someorder');
            }
        }
    }



    /**
     * 同步品牌
     */
    function syncBrand(){
        $cfbrand = array();
        //维修站点
        $sitePrefix = gp('sitePrefix');
        $sitePrefix = $sitePrefix?$sitePrefix:'es';
        $siteFirst = $this->common_model->selectFirst('mt_repair_site',array('site_prefix'=>$sitePrefix));
        if(!$siteFirst){
            show_error('未查到此站点'.$sitePrefix);
        }
        gsh_output('开始更新站点 '.$siteFirst['site_name'].' 的品牌数据');
        $brandList = $this->tomrepair->getBrandList();
        foreach ($brandList as $rs) {
            $brandFirst = $this->brand_model->selectFirst('maintain_brand',array('brand_name'=>$rs['label']));
            $inData = array(
                'site_id'=>$siteFirst['id'],'brand_id_from_site'=>$rs['value'],'brand_name_lang'=>$rs['label']
            );
            if($brandFirst){
                $inData['brand_id'] = $brandFirst['brand_id'];
            }else{
                $rsid = $this->brand_model->addOrSetBrand(array('brand_name'=>$rs['label']));
                $inData['brand_id'] = $rsid;
            }
            //判断是否已经存在
            $where = array('site_id'=>$siteFirst['id'],'brand_name_lang'=>$rs['label']);
            $siteBrandFirst = $this->brand_model->selectFirst('maintain_brand_site',$where);
            if(!$siteBrandFirst){
                $this->brand_model->insert('maintain_brand_site',$inData);
            }else{
                $cfbrand[] = $rs;
                $this->brand_model->update('maintain_brand_site',$inData,$where);
            }
            gsh_output('品牌：'.$rs['label'].'更新完成!');
        }
        gsh_output($siteFirst['site_name'].'........重复的品牌有....');
        gsh_output($cfbrand);
    }





}
